﻿using IBLL;
using IDAL;
using Models;
using Models.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{ 
    /// <summary>
    /// 订单详情表业务逻辑层--实现
    /// </summary>
    public class OrderDetaIlsBLL : IOrderDetaIlsBLL
    {

        #region 构造函数
        private IOrderDetaIlsDAL _orderDetaIlsDAL;
        private UnmanContexts _dbContext;   //数据上下文

        public OrderDetaIlsBLL(
            UnmanContexts dbContext
            , IOrderDetaIlsDAL orderDetaIlsDAL
            )
        {
            _dbContext = dbContext;
            _orderDetaIlsDAL = orderDetaIlsDAL;
        }

       
        #endregion

        #region 获取订单详情列表（GetOrderDetaIls）
        /// <summary>
        /// 获取订单详情列表
        /// </summary>
        /// <param name="page">第几页</param>
        /// <param name="limit">每页几条数据</param>
        /// <param name="orderInfoId">订单id</param>
        /// <param name="count">数据总数</param>
        /// <returns></returns>
        public List<GetOrderDetaIlsDTO> GetOrderDetaIls(int page, int limit, string orderInfoId, out int count)
        {
            //linq方法进行链表
            var data = from o in _dbContext.OrderDetaIls.Where(o => o.IsDalete == false)

                       join ol in _dbContext.OrderInfo.Where(ol => ol.IsDalete == false) //订单表
                       on o.OrderInfoId equals ol.Id
                       into tempOOL
                       from ool in tempOOL.DefaultIfEmpty()

                       join c in _dbContext.CommodityInfo.Where(c => c.IsDalete == false) //商品信息表
                       on o.ProductId equals c.Id
                       into tempOC
                       from oc in tempOC.DefaultIfEmpty()

                       select new GetOrderDetaIlsDTO
                       {
                           Id = o.Id,
                           Description = o.Description,
                           CreateTime = o.CreateTime,

                           OrderInfoId = ool.Id,
                           UserName = ool.UserName,

                           ProductId = oc.Id,
                           CommodityName = oc.CommodityName
                       };
            //订单id不为空
            if (!string.IsNullOrWhiteSpace(orderInfoId))
            {
                data = data.Where(o => o.OrderInfoId.Contains(orderInfoId));
            }
            //求总数
            count = data.Count();
            //分页
            var listPage = data.OrderByDescending(u => u.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
            //返回结果
            return listPage;
        }

        #endregion

        #region  新增订单详情的方法（CreateOrderDetaIls）
        public bool CreateOrderDetaIls(OrderDetaIls orderDetaIl, out string msg)
        {
            //非空判断
            if (string.IsNullOrWhiteSpace(orderDetaIl.OrderInfoId))
            {
                msg = "订单id不能为空！";
                return false;
            }
            //查询判断是否重复
            OrderDetaIls oldEntity = _orderDetaIlsDAL.GetEntities().FirstOrDefault(o => o.OrderInfoId == orderDetaIl.OrderInfoId);
            if (oldEntity != null)
            {
                msg = "订单id已经存在！";
                return false;
            }
            //给对象补全属性
            orderDetaIl.Id = Guid.NewGuid().ToString();
            orderDetaIl.CreateTime = DateTime.Now;
            //提交到数据库
            bool isSuccess = _orderDetaIlsDAL.CreateEntity(orderDetaIl);
            //返回结果
            msg = isSuccess ? "添加订单详情成功" : "添加订单详情失败";
            return isSuccess;
        }


        #endregion

        #region 更新订单详情信息的方法（UpdateOrderDetaIls）
        /// <summary>
        /// 更新订单详情信息的接口
        /// </summary>
        /// <param name="orderDetaIls">要更新的实体</param>
        /// <param name="msg">返回的描述信息</param>
        /// <returns></returns>
        public bool UpdateOrderDetaIls(OrderDetaIls orderDetaIls, out string msg)
        {
            #region 非空判断
            //非空判断
            if (string.IsNullOrWhiteSpace(orderDetaIls.Id))
            {
                msg = "Id不能为空！";
                return false;
            }
            //先判断这个订单Id非空信息
            if (string.IsNullOrWhiteSpace(orderDetaIls.OrderInfoId))
            {
                msg = "不能为空！";
                return false;
            }

            #endregion

            //判断这个id是否存在用户
            OrderDetaIls entity = _orderDetaIlsDAL.GetEntityById(orderDetaIls.Id);
            if (entity == null)
            {
                msg = "id无效";
                return false;
            }
            //先判断是不是跟原来的账号一样,一样表示没有修改
            if (entity.Description != orderDetaIls.OrderInfoId)
            {
                //通过账号查询数据库里是否存在相同的账号
                OrderDetaIls oldOrder = _orderDetaIlsDAL.GetEntities().FirstOrDefault(o => o.OrderInfoId == orderDetaIls.OrderInfoId);
                if (oldOrder != null)
                {
                    msg = "账号已被使用";
                    return false;
                }
            }

            //赋值
            entity.OrderInfoId = orderDetaIls.OrderInfoId;
            entity.Description = orderDetaIls.Description;
            entity.ProductId = orderDetaIls.ProductId;
            entity.CommodityName = orderDetaIls.CommodityName;

            //更新到数据库
            bool isOk = _orderDetaIlsDAL.UpdateEntity(entity);
            msg = isOk ? "修改成功" : "修改失败";
            //返回结果
            return isOk;
        }
        #endregion

        #region 订单详情软删除的方法（DeleteOrderDetaIls）
        public bool DeleteOrderDetaIls(string id)
        {
            //根据id查菜单是否存在
            OrderDetaIls  orderDetaIls = _orderDetaIlsDAL.GetEntityById(id);
            if (orderDetaIls == null)
            {
                return false;
            }
            //修改菜单的状态
            orderDetaIls.IsDalete = true;
            orderDetaIls.DeleteTime = DateTime.Now;
            //返回结果
            return _orderDetaIlsDAL.UpdateEntity(orderDetaIls);
        }
        #endregion

        #region 订单详情批量软删除的方法（DeleteOrderDetaIlss）
        /// <summary>
        /// 订单详情批量软删除的接口
        /// </summary>
        /// <param name="ids">被删除的订单详情id集合</param>
        /// <returns></returns>
        public bool DeleteOrderDetaIlss(List<string> ids)
        {
            foreach (var id in ids)
            {
                //根据订单Id查询用户
                OrderDetaIls orderDetaIls = _orderDetaIlsDAL.GetEntityById(id);
                if (orderDetaIls == null)
                {
                    continue;//跳出本次循环
                }
                orderDetaIls.IsDalete = true;
                orderDetaIls.DeleteTime = DateTime.Now;
                //执行修改
                _orderDetaIlsDAL.UpdateEntity(orderDetaIls);
            }
            return true;
        }
        #endregion

    }
}
